function [sbr,c] = sbr_envelope( sbr, bits )
%Table 4.72 – Syntax of sbr_envelope()

%Input
bs_amp_res = sbr.data.bs_amp_res;
bs_num_env = sbr.data.bs_num_env;
bs_df_env = sbr.data.bs_df_env;
bs_freq_res = sbr.data.bs_freq_res;
num_env_bands = sbr.freq_tables.n;

%Tables
t_huffman_env_1_5dB = [...
    [   1,   2 ];    [ -64, -65 ];    [   3,   4 ];    [ -63, -66 ];...
    [   5,   6 ];    [ -62, -67 ];    [   7,   8 ];    [ -61, -68 ];...
    [   9,  10 ];    [ -60, -69 ];    [  11,  12 ];    [ -59, -70 ];...
    [  13,  14 ];    [ -58, -71 ];    [  15,  16 ];    [ -57, -72 ];...
    [  17,  18 ];    [ -73, -56 ];    [  19,  21 ];    [ -74,  20 ];...
    [ -55, -75 ];    [  22,  26 ];    [  23,  24 ];    [ -54, -76 ];...
    [ -77,  25 ];    [ -53, -78 ];    [  27,  34 ];    [  28,  29 ];...
    [ -52, -79 ];    [  30,  31 ];    [ -80, -51 ];    [  32,  33 ];...
    [ -83, -82 ];    [ -81, -50 ];    [  35,  57 ];    [  36,  40 ];...
    [  37,  38 ];    [ -88, -84 ];    [ -48,  39 ];    [ -90, -85 ];...
    [  41,  46 ];    [  42,  43 ];    [ -49, -87 ];    [  44,  45 ];...
    [ -89, -86 ];    [-124,-123 ];    [  47,  50 ];    [  48,  49 ];...
    [-122,-121 ];    [-120,-119 ];    [  51,  54 ];    [  52,  53 ];...
    [-118,-117 ];    [-116,-115 ];    [  55,  56 ];    [-114,-113 ];...
    [-112,-111 ];    [  58,  89 ];    [  59,  74 ];    [  60,  67 ];...
    [  61,  64 ];    [  62,  63 ];    [-110,-109 ];    [-108,-107 ];...
    [  65,  66 ];    [-106,-105 ];    [-104,-103 ];    [  68,  71 ];...
    [  69,  70 ];    [-102,-101 ];    [-100, -99 ];    [  72,  73 ];...
    [ -98, -97 ];    [ -96, -95 ];    [  75,  82 ];    [  76,  79 ];...
    [  77,  78 ];    [ -94, -93 ];    [ -92, -91 ];    [  80,  81 ];...
    [ -47, -46 ];    [ -45, -44 ];    [  83,  86 ];    [  84,  85 ];...
    [ -43, -42 ];    [ -41, -40 ];    [  87,  88 ];    [ -39, -38 ];...
    [ -37, -36 ];    [  90, 105 ];    [  91,  98 ];    [  92,  95 ];...
    [  93,  94 ];    [ -35, -34 ];    [ -33, -32 ];    [  96,  97 ];...
    [ -31, -30 ];    [ -29, -28 ];    [  99, 102 ];    [ 100, 101 ];...
    [ -27, -26 ];    [ -25, -24 ];    [ 103, 104 ];    [ -23, -22 ];...
    [ -21, -20 ];    [ 106, 113 ];    [ 107, 110 ];    [ 108, 109 ];...
    [ -19, -18 ];    [ -17, -16 ];    [ 111, 112 ];    [ -15, -14 ];...
    [ -13, -12 ];    [ 114, 117 ];    [ 115, 116 ];    [ -11, -10 ];...
    [  -9,  -8 ];    [ 118, 119 ];    [  -7,  -6 ];    [  -5,  -4 ]];
f_huffman_env_1_5dB = [...
    [   1,   2 ];    [ -64, -65 ];    [   3,   4 ];    [ -63, -66 ];...
    [   5,   6 ];    [ -67, -62 ];    [   7,   8 ];    [ -68, -61 ];...
    [   9,  10 ];    [ -69, -60 ];    [  11,  13 ];    [ -70,  12 ];...
    [ -59, -71 ];    [  14,  16 ];    [ -58,  15 ];    [ -72, -57 ];...
    [  17,  19 ];    [ -73,  18 ];    [ -56, -74 ];    [  20,  23 ];...
    [  21,  22 ];    [ -55, -75 ];    [ -54, -53 ];    [  24,  27 ];...
    [  25,  26 ];    [ -76, -52 ];    [ -77, -51 ];    [  28,  31 ];...
    [  29,  30 ];    [ -50, -78 ];    [ -79, -49 ];    [  32,  36 ];...
    [  33,  34 ];    [ -48, -47 ];    [ -80,  35 ];    [ -81, -82 ];...
    [  37,  47 ];    [  38,  41 ];    [  39,  40 ];    [ -83, -46 ];...
    [ -45, -84 ];    [  42,  44 ];    [ -85,  43 ];    [ -44, -43 ];...
    [  45,  46 ];    [ -88, -87 ];    [ -86, -90 ];    [  48,  66 ];...
    [  49,  56 ];    [  50,  53 ];    [  51,  52 ];    [ -92, -42 ];...
    [ -41, -39 ];    [  54,  55 ];    [-105, -89 ];    [ -38, -37 ];...
    [  57,  60 ];    [  58,  59 ];    [ -94, -91 ];    [ -40, -36 ];...
    [  61,  63 ];    [ -20,  62 ];    [-115,-110 ];    [  64,  65 ];...
    [-108,-107 ];    [-101, -97 ];    [  67,  89 ];    [  68,  75 ];...
    [  69,  72 ];    [  70,  71 ];    [ -95, -93 ];    [ -34, -27 ];...
    [  73,  74 ];    [ -22, -17 ];    [ -16,-124 ];    [  76,  82 ];...
    [  77,  79 ];    [-123,  78 ];    [-122,-121 ];    [  80,  81 ];...
    [-120,-119 ];    [-118,-117 ];    [  83,  86 ];    [  84,  85 ];...
    [-116,-114 ];    [-113,-112 ];    [  87,  88 ];    [-111,-109 ];...
    [-106,-104 ];    [  90, 105 ];    [  91,  98 ];    [  92,  95 ];...
    [  93,  94 ];    [-103,-102 ];    [-100, -99 ];    [  96,  97 ];...
    [ -98, -96 ];    [ -35, -33 ];    [  99, 102 ];    [ 100, 101 ];...
    [ -32, -31 ];    [ -30, -29 ];    [ 103, 104 ];    [ -28, -26 ];...
    [ -25, -24 ];    [ 106, 113 ];    [ 107, 110 ];    [ 108, 109 ];...
    [ -23, -21 ];    [ -19, -18 ];    [ 111, 112 ];    [ -15, -14 ];...
    [ -13, -12 ];    [ 114, 117 ];    [ 115, 116 ];    [ -11, -10 ];...
    [  -9,  -8 ];    [ 118, 119 ];    [  -7,  -6 ];    [  -5,  -4 ]];
t_huffman_env_3_0dB = [...
    [ -64,   1 ];    [ -65,   2 ];    [ -63,   3 ];    [ -66,   4 ];...
    [ -62,   5 ];    [ -67,   6 ];    [ -61,   7 ];    [ -68,   8 ];...
    [ -60,   9 ];    [  10,  11 ];    [ -69, -59 ];    [  12,  14 ];...
    [ -70,  13 ];    [ -71, -58 ];    [  15,  18 ];    [  16,  17 ];...
    [ -72, -57 ];    [ -73, -74 ];    [  19,  22 ];    [ -56,  20 ];...
    [ -55,  21 ];    [ -54, -77 ];    [  23,  31 ];    [  24,  25 ];...
    [ -75, -76 ];    [  26,  27 ];    [ -78, -53 ];    [  28,  29 ];...
    [ -52, -95 ];    [ -94,  30 ];    [ -93, -92 ];    [  32,  47 ];...
    [  33,  40 ];    [  34,  37 ];    [  35,  36 ];    [ -91, -90 ];...
    [ -89, -88 ];    [  38,  39 ];    [ -87, -86 ];    [ -85, -84 ];...
    [  41,  44 ];    [  42,  43 ];    [ -83, -82 ];    [ -81, -80 ];...
    [  45,  46 ];    [ -79, -51 ];    [ -50, -49 ];    [  48,  55 ];...
    [  49,  52 ];    [  50,  51 ];    [ -48, -47 ];    [ -46, -45 ];...
    [  53,  54 ];    [ -44, -43 ];    [ -42, -41 ];    [  56,  59 ];...
    [  57,  58 ];    [ -40, -39 ];    [ -38, -37 ];    [  60,  61 ];...
    [ -36, -35 ];    [ -34, -33 ]];
f_huffman_env_3_0dB = [...
    [ -64,   1 ];    [ -65,   2 ];    [ -63,   3 ];    [ -66,   4 ];...
    [ -62,   5 ];    [ -67,   6 ];    [   7,   8 ];    [ -61, -68 ];...
    [   9,  10 ];    [ -60, -69 ];    [  11,  12 ];    [ -59, -70 ];...
    [  13,  14 ];    [ -58, -71 ];    [  15,  16 ];    [ -57, -72 ];...
    [  17,  19 ];    [ -56,  18 ];    [ -55, -73 ];    [  20,  24 ];...
    [  21,  22 ];    [ -74, -54 ];    [ -53,  23 ];    [ -75, -76 ];...
    [  25,  30 ];    [  26,  27 ];    [ -52, -51 ];    [  28,  29 ];...
    [ -77, -79 ];    [ -50, -49 ];    [  31,  39 ];    [  32,  35 ];...
    [  33,  34 ];    [ -78, -46 ];    [ -82, -88 ];    [  36,  37 ];...
    [ -83, -48 ];    [ -47,  38 ];    [ -86, -85 ];    [  40,  47 ];...
    [  41,  44 ];    [  42,  43 ];    [ -80, -44 ];    [ -43, -42 ];...
    [  45,  46 ];    [ -39, -87 ];    [ -84, -40 ];    [  48,  55 ];...
    [  49,  52 ];    [  50,  51 ];    [ -95, -94 ];    [ -93, -92 ];...
    [  53,  54 ];    [ -91, -90 ];    [ -89, -81 ];    [  56,  59 ];...
    [  57,  58 ];    [ -45, -41 ];    [ -38, -37 ];    [  60,  61 ];...
    [ -36, -35 ];    [ -34, -33 ]];
if bs_amp_res
    t_huff = t_huffman_env_3_0dB;
    f_huff = f_huffman_env_3_0dB;
else
    t_huff = t_huffman_env_1_5dB;
    f_huff = f_huffman_env_1_5dB;
end

%Init
c = 0;
bs_data_env = cell(1,bs_num_env);

%Huffman decoding
for env=1:bs_num_env
    if bs_df_env(env)==0
        if bs_amp_res
            bs_data_env{env}(1) = bits2int( bits(c+1:c+6) );
            c = c + 6;
        else
            bs_data_env{env}(1) = bits2int( bits(c+1:c+7) );
            c = c + 7;
        end
        for band=2:num_env_bands(bs_freq_res(env)+1)
            [bs_data_env{env}(band),decoded_bits] = sbr_huff_dec(bits(c+1:end), f_huff);
            c = c + decoded_bits;
        end
    else
        for band=1:num_env_bands(bs_freq_res(env)+1)
            [bs_data_env{env}(band),decoded_bits] = sbr_huff_dec(bits(c+1:end), t_huff);
            c = c + decoded_bits;
        end
    end
end

%Output
sbr.data.bs_data_env = bs_data_env;